use std::sync::mpsc;
use std::thread;


pub trait OffThreadExt:Iterator {
    fn off_thread(self) -> mpsc::IntoIter<Self::Item>;
}

impl<T> OffThreadExt for T 
where T:Iterator + Send + 'static,
    T::Item: Send + 'static
{
    fn off_thread(self) -> mpsc::IntoIter<Self::Item> {
        let (tx, rx) = mpsc::sync_channel(1024);
        thread::spawn(move || {
            for item in self {
                if tx.send(item).is_err() {
                    break;
                }
            }
        });
        rx.into_iter()
    }
}